package com.airforce.service;
import com.airforce.config.Redisconfig;
import com.airforce.controller.Login;
import com.airforce.mappers.sys_userMapper;
import com.airforce.model.sys_user;
import com.airforce.utils.MD5BASE64Util;
import com.airforce.utils.ResultCode;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Date;


/**
 *
 * 基于JWT的登入认证模块
 */
@Slf4j
@Service
public   class LoginAuthentication {


    @Value("${salt}")
    private String salt;

    @Autowired
    private sys_userMapper sys_userMapper;

    @Autowired
    Redisconfig redisconfig;


    private  final static Logger logger=LoggerFactory.getLogger(LoginAuthentication.class);

    public  Object loginAuthentication(sys_user sys_user) {


        try {

            sys_user u=sys_userMapper.selectbyname(sys_user.getUsername());
                if (u != null) {

                    String parm_passwd= MD5BASE64Util.md5base64encode(sys_user.getPassword()+sys_user.getUsername()+salt);  //密码加密
//                    System.out.println("加密后的密码："+parm_passwd+"库里的："+u.getPassword());
//                    System.out.println("    "+sys_user.getPassword()+"     "+sys_user.getUsername());
                    if (parm_passwd.equals(u.getPassword())) {
                        String token=MD5BASE64Util.base64encode(sys_user.getUsername()+","+salt+","+new Date().getTime());
//                        String token = jwt.create(u);
//                        log.info(token);
//                        Claims body = (Claims) jwt.parse(token);  //获取jwt中的数据
//                        String create_time = body.get("create_time").toString();  //获取jwt中的时间戳用于可视化redis 存储的JWT键值
//                        ld.setJwt(token);
//                        ld.setZone(u.getZone());
//                        Integer expire = RedisConstant.EXPIRE;
//                        //设置jwt至redis
//                        RedisTemplate.opsForValue().set(String.format(RedisConstant.JWT_PREFIX, user.getAccount(), create_time), ld.getJwt(), expire, TimeUnit.SECONDS);
                        Jedis jedis=new Jedis(redisconfig.getHost(),redisconfig.getPort());
                        jedis.set(sys_user.getUsername()+"_admintoken",token);
                       logger.info("管理员："+sys_user.getUsername()+"登入系统。");
                        return ResultCode.SUCCESS.result(token);
                    } else {
                        return ResultCode.NO_USERPWD.result();
                    }

                } else {
                    return ResultCode.NO_USERPWD.result();
                }

        } catch (SignatureException e) {

            return ResultCode.JWT_ERROR.result();
        }


    }

    public static void main(String agrs[]){
      String str = MD5BASE64Util.md5base64encode("root123456salt");
        System.out.println(str);
    }

}